package org.trackcore.handler;

import org.trackcore.model.entity.BehaviorPageAccess;
import org.trackcore.service.BehaviorPageAccessService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class PageAccessDurationHandler implements DataHandler {
    private final BehaviorPageAccessService behaviorPageAccessService;

    public PageAccessDurationHandler(BehaviorPageAccessService behaviorPageAccessService) {
        this.behaviorPageAccessService = behaviorPageAccessService;
    }

    @Override
    public String getHandlerKey() {
        return "behavior_page-access-duration"; // 对应前端subType
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        BehaviorPageAccess access = BehaviorPageAccess.builder()
                .logId(logId)
                .pageUrl(DataUtils.getString(data, "pageURL"))
                .duration(DataUtils.getLong(data, "duration"))
                .uuid(DataUtils.getString(data, "uuid"))
                .duration(DataUtils.getLong(data, "duration"))
                .build();

        behaviorPageAccessService.save(access);
    }
}
